package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据初始化监听器
 */
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    //spring容器创建完成（aop ioc di）之后马上执行
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("--------------------vo-start-------------------------");
        //1: 将攻略所有数据查询出来（点赞数， 回复数，收藏数，分享数，评论数）
        List<Strategy> list =  strategyService.list();
        //2:构建成vo对象存到redis中
        for (Strategy strategy : list) {

            if(strategyStatisVORedisService.isVoExsits(strategy.getId())){
                continue;
            }

            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());  //id设置

            //添加到redis中
            strategyStatisVORedisService.setStrategyStatisVo(vo);
        }
        System.out.println("--------------------vo-end-------------------------");
    }
}
